//1. 公用样式
require('../../assets/css/index.less');
require('../../assets/css/basic.less');
require('../../assets/fonts/iconfont.css');
//2. 当前页面样式
require('./circleadd.less');
require('../../lib/swiper/swiper-bundle.css');
// 导入weui
require("../../lib/weui/weui.css")
const weui = require("../../lib/weui/weui.js")


//3. 公用js
let dom = require('../../utils/dom.js');
let https = require('../../utils/https.js');
let local = require('../../utils/local.js');
require('../../lib/swiper/swiper-bundle.js');


dom.ready(function () {
    // 调用回退的封装函数
    dom.goback()

    // 定义省份id
    let provinceId;
    dom.getEle('#showPicker').addEventListener('click', function () {

        // 获取所有省份信息----ajax
        https.get('/api/shared/province', {}, function (res) {
            // 判定
            if (res.errno == 0) {
                // console.log(res.data);
                /*
                   注意：格式必须为
                     label: '四川',
                     value: 0
                */
                let preData = res.data.map(v => {
                    return {
                        label: v.name,
                        value: v.id
                    }
                })
                // console.log(preData);

                // 调用选择器
                weui.picker(preData, {
                    onConfirm: function (result) {
                        // 简便写法,利用下标0
                        dom.getEle('#province').innerHTML = result[0].label;


                        // 先获取省份id
                        provinceId = result[0].value
                        // console.log(provinceId)

                    },
                    title: '省份'
                });

            }
        })

    });


    // 进行城市选择
    let cityId;
    dom.getEle('#showCityPicker').addEventListener('click', function () {
        if (provinceId) {
            // 获取数据
            https.get('/api/shared/city', { provinceId }, function (res) {
                if (res.errno == 0) {
                    console.log(res.data);
                    // 数据格式处理
                    let cArr = res.data.map(v => {
                        return {
                            label: v.name,
                            value: v.id
                        }
                    })
                    // console.log(cArr);

                    // 选择器
                    weui.picker(cArr, {
                        onConfirm: function (result) {
                            dom.getEle('#city').innerHTML = result[0].label;
                            cityId = result[0].value
                        },
                        title: '城市'

                    })

                }
            })
        } else {
            console.log('请选择省份');
        }
    })


    //  上传圈子图片
    let imgArr = [];
    dom.getEle('#headFile').addEventListener('change', function () {
        // 获取当前上传图片的所有相关资源
        let f = this.files[0];//图片
        // console.log(f);

        // 组装拼接对象
        let fd = new FormData();
        fd.append('file', f);
        // console.log(fd.get('file'));

        // 发ajax
        https.upLoadPic('/api/shared/uploadPortrait1', fd, function (res) {
            console.log(res.data);
            if (res.errno == 0) {
                dom.getEle('#avatar').src = ` http://47.96.154.185:3701${res.data.url}`;
                imgArr.push(res.data.url);
            }
        })
    })

    console.log(imgArr);
    // 发表圈子
    dom.getEle('#saveBtn').addEventListener('click', function () {
        let data = {
            imgUrls: imgArr,
            provinceId,
            cityId,
            content: info.value,
        }

        https.post('/api/circles/opinion', data, function (res) {
            console.log(res.data);
            if (res.errno == 0) {
                // 提示信息
                weui.toast('发布成功', 3000);
                // 跳转到circle页面
                  window.location.href='circle.html'
            }
        })
    })

});
